package com.epcs.model.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

import com.epcs.model.dto.Account;
import com.epcs.model.dto.Email;
import com.epcs.util.DBUtil;

public class EmailDAO {

	public boolean insertEmailToDB(Email email) {

		Session session = null;
		Transaction transaction = null;
		try {

			// get session
			session = DBUtil.getSession();
			// open transaction
			transaction = session.beginTransaction();
			session.save(email);
			transaction.commit();

			return true;
		} catch (HibernateException e) {
			transaction.rollback();
			System.out.println("Rolback: \n" + e.getMessage());

		} finally {
			if (session != null) {
				session.close();
			}
		}
		return false;
	}
	public List<Email> getAllEmailFromDB(Account account) {
		
		Session session = null;
		try {
			
			// get session
			session = DBUtil.getSession();
			Criteria criteria = session.createCriteria(Email.class);
			Criterion criterion = Restrictions.eq("account", account);
			criteria.add(criterion);
			List<Email> emaiList = new ArrayList<Email>();
			emaiList = criteria.list();
			if (emaiList.size() > 0){
				return emaiList;
			}
			
		} catch (HibernateException e) {
			System.out.println("Rolback: \n" + e.getMessage());
			
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return null;
	}
	
	public Email getEmailById(int id) {
		
		Session session = null;
		try {
			
			// get session
			session = DBUtil.getSession();
			Criteria criteria = session.createCriteria(Email.class);
			Criterion criterion = Restrictions.eq("emailId", id);
			criteria.add(criterion);
			Email email = (Email)criteria.uniqueResult();

			return email;
			
		} catch (HibernateException e) {
			System.out.println("Rolback: \n" + e.getMessage());
			
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return null;
	}
	
	public Email getEmailFromDB(String emailAddress) {
		
		Session session = null;
		try {
			
			// get session
			session = DBUtil.getSession();
			Criteria criteria = session.createCriteria(Email.class);
			Criterion criterion = Restrictions.eq("email", emailAddress);
			criteria.add(criterion);
			
			return (Email)criteria.uniqueResult();
			
		} catch (HibernateException e) {
			System.out.println("Rolback: \n" + e.getMessage());
			
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return null;
	}
	public boolean updateEmailToDB(Email email) {
		Session session = null;
		Transaction transaction = null;
		try {

			// get session
			session = DBUtil.getSession();
			// open transaction
			transaction = session.beginTransaction();
			session.update(email);
			transaction.commit();

			return true;
		} catch (HibernateException e) {
			transaction.rollback();
			System.out.println("Rolback: \n" + e.getMessage());

		} finally {
			if (session != null) {
				session.close();
			}
		}
		return false;
	}
	
	public boolean deleteEmailFromDB(Email email) {
		Session session = null;
		Transaction transaction = null;
		try {
			
			// get session
			session = DBUtil.getSession();
			// open transaction
			transaction = session.beginTransaction();
			session.delete(email);
			transaction.commit();
			
			return true;
		} catch (HibernateException e) {
			transaction.rollback();
			System.out.println("Rolback: \n" + e.getMessage());
			
		} finally {
			if (session != null) {
				session.close();
			}
		}
		return false;
	}

}
